package br.com.gecandango.portlets.socio;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import br.com.gecandango.beans.Especialidade;
import br.com.gecandango.beans.Etapa;
import br.com.gecandango.beans.FichaSaude;
import br.com.gecandango.beans.Socio;

public class SocioPortlet extends SocioAction {

	static final Logger logger = Logger
	.getLogger(SocioPortlet.class);
	
	public void processAction(ActionMapping mapping, ActionForm form,
			PortletConfig config, ActionRequest req, ActionResponse res)
			throws Exception {
		String acao = req.getParameter("acao");
		Socio socio = new Socio();
		if (acao.equals("add")){
			setForward(req, "portlet.ext.socio.edit");
		} else if (acao.equals("list")){
			req.setAttribute("socios", getSocioService().list());
			setForward(req, "portlet.ext.socio.list");
		} else if (acao.equals("edit_socio")){
			socio = getSocioService().find(Long.parseLong(req
					.getParameter("id")));
			req.setAttribute("socio", socio);
			req.setAttribute("fichaSaude", socio.getFichaSaude());
			setForward(req, "portlet.ext.socio.edit");
		} else if (acao.equals("desassociar")){
			socio = getSocioService().find(Long.parseLong(req
					.getParameter("id")));
			getSocioService().delete(socio);
			setForward(req, "portlet.ext.socio.list");
		} else if (acao.equals("salvar")){
			socio = salvarSocio(req);
			req.setAttribute("socio", socio);
			req.setAttribute("fichaSaude", socio.getFichaSaude());
			setForward(req, "portlet.ext.socio.edit");
		} else {
			setForward(req, "portlet.ext.socio.view");
		}
		
		
	}
	public Socio salvarSocio(ActionRequest request){
		String id = (String) request.getParameter("id_socio");
		String nome = (String) request.getParameter("nome");
		String reg_ueb = (String) request.getParameter("reg_ueb");
		String endereco = (String) request.getParameter("endereco");
		String bairro = (String) request.getParameter("bairro");		
		String cidade = (String) request.getParameter("cidade");		
		String uf = (String) request.getParameter("uf");
		String cep = (String) request.getParameter("cep");		
		String telefone = (String) request.getParameter("telefone");
		String celular = (String) request.getParameter("celular");
		String email = (String) request.getParameter("email");
		String pai = (String) request.getParameter("pai");
		String mae = (String) request.getParameter("mae");
		String profissao_pai = (String) request.getParameter("profissao_pai");
		String profissao_mae = (String) request.getParameter("profissao_mae");
		String telefone_pai = (String) request.getParameter("telefone_pai");
		String telefone_mae = (String) request.getParameter("telefone_mae");
		String responsavel = (String) request.getParameter("responsavel");
		String profissao_responsavel = (String) request.getParameter("profissao_responsavel");
		String telefone_responsavel = (String) request.getParameter("telefone_responsavel");
		
		
		Socio socio = new Socio();
		
		if(id!= null){
			try {
				socio.setId(Long.parseLong(id));
			} catch (Exception e) {
			}
		}
		socio.setNome(nome);
		
		socio.setReg_ueb(reg_ueb);
		socio.setEndereco(endereco);
		socio.setBairro(bairro);		
		socio.setCidade(cidade);		
		socio.setUf(uf);
		socio.setCep(cep);		
		socio.setTelefone(telefone);
		socio.setCelular(celular);
		socio.setEmail(email);
		socio.setPai(pai);
		socio.setMae(mae);
		socio.setResponsavel(responsavel);
		socio.setProfissao_pai(profissao_pai);
		socio.setProfissao_mae(profissao_mae);
		socio.setProfissao_responsavel(profissao_responsavel);
		socio.setTelefone_pai(telefone_pai);
		socio.setTelefone_mae(telefone_mae);
		socio.setTelefone_responsavel(telefone_responsavel);
		socio.setFichaSaude(salvarFichaSaude(request));
		try {
			return getSocioService().save(socio);
		} catch (Exception e) {
			return null;
		}		
	}
	public FichaSaude salvarFichaSaude(ActionRequest request){
		String id = (String) request.getParameter("id_ficha_saude");
		
		String tipoSanguineoFatorRH = (String) request.getParameter("tipoSanguineoFatorRH");

		
		FichaSaude fichaSaude = new FichaSaude();
		
		try {
			fichaSaude.setId(Long.parseLong(request.getParameter("id_ficha_saude")));
		} catch (Exception e) {
			
		}
		fichaSaude.setTipoSanguineoFatorRH(tipoSanguineoFatorRH);
		
		return fichaSaude;
	}
	
	
}
